//
//  MineViewController.m
//  便签
//
//  Created by  on 2022/9/28.
//

#import "MineViewController.h"
#import "SettingViewController.h"
#import "FavoriteViewController.h"
#import "HomeViewController.h"

@interface MineViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIButton *avatar;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *date;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgViewLeft;
@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet UIButton *btn3;
@property (nonatomic, strong) User *model;
@end

@implementation MineViewController
- (void)viewDidLoad{
    [super viewDidLoad];
    [self initUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:notifyUserUpdate object:nil];
}
- (void)actionForUpdateColor{
    self.bgView.backgroundColor = [UIColor colorHex:APP.mainColor];
}

- (void)initUI{
    self.bgViewLeft.constant = -self.view.frame.size.width;
    self.name.text = LS(self.model.account);
    int stamp = [[NSDate currentTimeStamp:@"yyyy/MM/dd"] intValue];
    self.date.text = [NSString stringWithFormat:@"%@ %@",[NSDate transToDate:@"yyyy/MM/dd" : stamp],LS([NSDate weakDay:[NSDate dateWithTimeIntervalSince1970:stamp]])];
    UIImage *img = self.model.headImg.length > 0 ? [UIImage imageWithData:self.model.headImg] : [UIImage imageNamed:@"默认头像"];
    [self.avatar setImage:img forState:UIControlStateNormal];
    [self actionForUpdateColor];
}
- (void)loadData{
    self.model = APP.user;
    [self initUI];
}
- (void)show{
    APP.meWindow.hidden = NO;
    self.bgViewLeft.constant = 0;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}
- (User *)model{
    if (!_model) {
        _model = APP.user;
    }
    return _model;
}
-(void)hidden:(nullable void (^)(void))block{
    self.bgViewLeft.constant = -self.view.frame.size.width;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        APP.meWindow.hidden = YES;
        if (block) {
            block();
        }
    }];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        return [self hidden:nil];
    }else if (sender.tag == 202){
        if ([self.model.account isEqualToString:@"去登录"]) {
            LoginViewController *vc = [LoginViewController new];
            vc.isLogin = YES;
            [self hidden:^{
                [[self currentVC].navigationController pushViewController:vc animated:NO];
            }];
            return;
        }
        WS(self)
        SheetViewController *vc = [SheetViewController new];
        vc.withCancel = NO;
        vc.array = @[@"相机",@"从图库中选择"];
        vc.block = ^(int index, NSString * _Nonnull text) {
            [weakself ImagePicker:(UIImagePickerControllerSourceType)index == 0 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary];
        };
        [self presentViewController:vc animated:NO completion:nil];
    } else{
        sender.selected = YES;
        self.btn1.selected = [self.btn1 isEqual:sender];
        self.btn2.selected = [self.btn2 isEqual:sender];
        self.btn3.selected = [self.btn3 isEqual:sender];
        self.btn1.bgHexColor = self.btn1.selected ? @"#FD9C14" : @"#FFFFFF";
        self.btn2.bgHexColor = self.btn2.selected ? @"#FD9C14" : @"#FFFFFF";
        self.btn3.bgHexColor = self.btn3.selected ? @"#FD9C14" : @"#FFFFFF";
        if (sender.tag == 203) {
            HomeViewController *vc = [HomeViewController new];
            BaseNavigationController *navc = [[BaseNavigationController alloc] initWithRootViewController:vc];
            navc.modalPresentationStyle = UIModalPresentationFullScreen;
            [self hidden:^{
                [[self currentVC].navigationController pushViewController:vc animated:NO];
            }];
        }else if (sender.tag == 204){
            FavoriteViewController *vc = [FavoriteViewController new];
            vc.title = LS(@"收藏");
            vc.isedit = YES;
            BaseNavigationController *navc = [[BaseNavigationController alloc] initWithRootViewController:vc];
            navc.modalPresentationStyle = UIModalPresentationFullScreen;
            [self hidden:^{
                [[self currentVC].navigationController pushViewController:vc animated:NO];
            }];
        }else if (sender.tag == 205){
            SettingViewController *vc = [SettingViewController new];
            BaseNavigationController *navc = [[BaseNavigationController alloc] initWithRootViewController:vc];
            navc.modalPresentationStyle = UIModalPresentationFullScreen;
            [self hidden:^{
                [[self currentVC].navigationController pushViewController:vc animated:NO];
            }];
        }
    }
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UIView *view = [self firstResponder:self.view];
    if (view != nil) {
        [super touchesEnded:touches withEvent:event];
        [self.view endEditing:YES];
    }else{
        UITouch *touch = [[touches allObjects] lastObject];
        if ([touch.view isDescendantOfView:self.view.subviews.firstObject] == NO) {
            [self hidden:nil];
        }
    }
}

#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{
        [self hidden:nil];
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    NSData *data = UIImageJPEGRepresentation(img, 0.5);
    self.model.headImg = data;
    WS(self)
    [self.model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself initUI];
        });
    }];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        [self hidden:nil];
    }];
}
@end

